package chapter08

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test01_Patter
 * Package: chapter08
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-29
 *          Time: 20:41
 */

object Test01_Patter {
  def main(args: Array[String]): Unit = {
    //模式匹配基本语法
    val x: Int = 10
    val y: String = x match {
      case 1 => "noe"
      case 2 => "two"
      case 3 => "three"
      case _ => "other"
      //代表其他的情况
    }
    println(s"y = ${y}")


    //实例 传字符 实现二元运算
    val a = 25
    val b = 13

    def matchDuaOp(op: Char) = op match {
      case '+' => a + b
      case '-' => a - b
      case '/' => a / b
      case '*' => a * b
      case '%' => a % b

      case _ => "非法运算"
    }

    println(matchDuaOp('+'))
    println(matchDuaOp('-'))
    println(matchDuaOp('*'))
    println(matchDuaOp('^'))

    println("-------------------------------")

    //模式守卫
    //求一个数的绝对值
    def abs(num: Int): Int = {
      num match {
        //没有具体的值 定义的变量来 判断
        case i if i >= 0 => i
        case i if i < 0 => -i
      }
    }
    println(abs(0))
    println(abs(-1))
    println(abs(10))

  }
}
